﻿using FPSO.Models.Constant;
using FPSO.Models.TagMapping;
using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Windows.Media;

namespace FPSO.Models.ViewModel.Controls
{
    public class PumpViewModel : NotificationObject
    {
        public PumpViewModel(string pumpId)
        {
            PumpId = pumpId;
			maxOpenPress = TankConstant.MAXPUMPPRESS;
			tarPress = 0;
			openPress = 0;
			pumpStatus = 0;
			SetPumpStatus();
        }
        public string PumpId { get; private set; }

		private int maxOpenPress;
		public int MaxOpenPress { get { return maxOpenPress; } set { maxOpenPress = value; RaisePropertyChanged("MaxOpenPress"); } }

        private int openPress;
        [FieldMapper("OPENPRESS", TypeCode.Int32, 1d)]
        public int OpenPress
        {
            get { return openPress; }
            set
            {
                openPress = value;
				SetPumpStatus();
				RaisePropertyChanged("OpenPress");
            }
        }

		private int tarPress;
		public int TarPress
		{
			get { return tarPress; }
			set
			{
				tarPress = value;
				SetPumpStatus();
				RaisePropertyChanged("TarRatio");
			}
		}

		private double openRatio;
		public double OpenRatio
		{
			get { return openRatio; }
			set
			{
				openRatio = value;
				RaisePropertyChanged("OpenRatio");
			}
		}

		private double openRatioHeight;
		public double OpenRatioHeight
		{
			get { return openRatioHeight; }
			set
			{
				openRatioHeight = value;
				RaisePropertyChanged("OpenRatioHeight");
			}
		}

		private double tarRatioHeight;
		public double TarRatioHeight
		{
			get { return tarRatioHeight; }
			set
			{
				tarRatioHeight = value;
				RaisePropertyChanged("TarRatioHeight");
			}
		}

		private bool running;
		[FieldMapper("RUNNING", TypeCode.Int32, 1d)]
		public bool Running
		{
			get { return running; }
			set
			{
				running = value;
				RaisePropertyChanged("Running");
			}
		}

		private int runningStatus;
        //[FieldMapper("RUNNING", TypeCode.Int32, 1d)]
        public int RunningStatus
        {
            get { return runningStatus; }
            set
            {
				runningStatus = value;
                RaisePropertyChanged("RunningStatus");
            }
        }

		private int pumpStatus;
		public int PumpStatus
		{
			get { return pumpStatus; }
			set
			{
				pumpStatus = value;
				SetPumpStatus();
				RaisePropertyChanged("PumpStatus");
			}
		}
		private void SetPumpStatus()
		{
			if(pumpStatus == 0)
			{
				tarPress = 0;
			}
			OpenRatio = Convert.ToDouble(openPress) / maxOpenPress * 100;
			OpenRatioHeight = Convert.ToDouble(openPress) / maxOpenPress * 300;
			TarRatioHeight = Convert.ToDouble(tarPress) / maxOpenPress * 300;
			RunningStatus = 1;
			if (openPress > 0)
				RunningStatus = 0;

			Running = false;
			if(RunningStatus == 0)
				Running = true;
		}
	}
}
